<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 4.3.2 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter Model Class
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Libraries
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/libraries/config.html
 */
class Model {

	var $_parent_name = '';

	public $_table_name = false;
	public $_primary_key = false;
	public $_ignore_vars;
	/**
	 * Constructor
	 *
	 * @access public
	 */
	function Model()
	{
		// If the magic __get() or __set() methods are used in a Model references can't be used.
		$this->_assign_libraries( (method_exists($this, '__get') OR method_exists($this, '__set')) ? FALSE : TRUE );
		
		// We don't want to assign the model object to itself when using the
		// assign_libraries function below so we'll grab the name of the model parent
		$this->_parent_name = ucfirst(get_class($this));
		
		log_message('debug', "Model Class Initialized");
		
		
		$this->_ignore_vars = array('_parent_name', '_table_name', '_primary_key', '_ignore_vars');
	}

	/**
	 * Assign Libraries
	 *
	 * Creates local references to all currently instantiated objects
	 * so that any syntax that can be legally used in a controller
	 * can be used within models.  
	 *
	 * @access private
	 */	
	function _assign_libraries($use_reference = TRUE)
	{
		$CI =& get_instance();				
		foreach (array_keys(get_object_vars($CI)) as $key)
		{
			if ( ! isset($this->$key) AND $key != $this->_parent_name)
			{			
				// In some cases using references can cause
				// problems so we'll conditionally use them
				if ($use_reference == TRUE)
				{
					$this->$key = NULL; // Needed to prevent reference errors with some configurations
					$this->$key =& $CI->$key;
				}
				else
				{
					$this->$key = $CI->$key;
				}
			}
		}		
	}





    public function save($keyFalse = true)
    {
        
		$aClassVars = get_class_vars(get_class($this));
		
		$dbArray = array();
		
		foreach ($aClassVars as $vVar => $vVal)
		{

			if (in_array($vVar, $this->_ignore_vars) === false)
			{
				if ($this->$vVar !== false)
				{
					$dbArray[$vVar] = $this->$vVar;
				}		
			}
		}	
		

		
		$sPrimaryKeyVar = $this->_primary_key;
		
		if ($this->$sPrimaryKeyVar !== false)
		{
			$this->db->where($this->_primary_key, $this->$sPrimaryKeyVar);
			$this->db->update($this->_table_name, $dbArray);
			
			if ($keyFalse === true)
			{
				$this->$sPrimaryKeyVar = false;
			}
			
			return $this->$sPrimaryKeyVar;
		}
		
		$this->db->insert($this->_table_name, $dbArray);
		return $this->db->insert_id();
	}	
	
	
    public function delete($keyFalse = true)
    {
       $sPrimaryKeyVar = $this->_primary_key;
	   
	    if ($this->$sPrimaryKeyVar !== false)
		{
			$this->db->where($this->_primary_key, $this->$sPrimaryKeyVar);
			$this->db->delete($this->_table_name);
			
			if ($keyFalse === true)
			{
				$this->$sPrimaryKeyVar = false;
			}
			
			return $this->db->affected_rows();
		}
		
		return false;
    }	
	
	
	public function get($aOptions = array(), $iLimit = 0, $iOffset = 0)
	{
		if (is_numeric($aOptions) === true)
		{
			$iLimit = $aOptions;
			$aOptions = array();
		}
		
		$aClassVars = get_class_vars(get_class($this));
		
		$whereArray = array();
		
		foreach ($aClassVars as $vVar => $vVal)
		{
			if (in_array($vVar, $this->_ignore_vars) === false)
			{
				if ($this->$vVar !== false)
				{
					$whereArray[$vVar] = $this->$vVar;
				}		
			}
		}
		
		
		$this->db->select();
		$this->db->from($this->_table_name);
		
		if (count($whereArray) > 0)
		{
			$this->db->where($whereArray);
		}
		
		
		if (count($aOptions) > 0)
		{
			foreach ($aOptions as $sFuncCall => $aParams)
			{
				if ((is_array($aParams) === true) && ($sFuncCall !== 'where_in'))
				{
					foreach ($aParams as $vVar => $vVal)
					{
						if (is_array($vVal) === false)
						{
							if (is_numeric($sFuncCall) === true)
							{
								$this->db->$vVar($vVal);	
							}
							else 
							{
								$this->db->$sFuncCall($vVar, $vVal);	
							}
						}
						else 
						{
							
							
							if (is_numeric($sFuncCall) === true)
							{
								call_user_func_array(array($this->db, $vVar), $vVal);
							}
							else 
							{
								array_unshift($vVal, $vVar); 
								
								call_user_func_array(array($this->db, $sFuncCall), $vVal);
							}
	
						}
					}
				}
				else 
				{
					$this->db->$sFuncCall($aParams);	
				}
				
			}
		}
		
		if ($iLimit > 0)
		{
			$this->db->limit($iLimit, $iOffset);
		}
		
		
		$qRet = $this->db->get();
		

		
		return $qRet;
						
	}


	public function get_max() 
	{
		$aClassVars = get_class_vars(get_class($this));
		
		foreach ($aClassVars as $vVar => $vVal)
		{
			if (in_array($vVar, $this->_ignore_vars) === false)
			{
				if ($this->$vVar !== false) 
				{
					$this->db->select_max($vVar, 'max_val');
						
				}
			}
		}
		
		$this->db->from($this->_table_name);
		$this->db->limit(1);
		
		$qRet = $this->db->get();
		
		return $qRet->row(0)->max_val;
	}
	
	
	
	public function get_count() 
	{
		$aClassVars = get_class_vars(get_class($this));
		
		foreach ($aClassVars as $vVar => $vVal)
		{
			if (in_array($vVar, $this->_ignore_vars) === false)
			{
				if ($this->$vVar !== false) 
				{
					$this->db->where($vVar, $this->$vVar);
						
				}
			}
		}
		
		$this->db->from($this->_table_name);
		
		return $this->db->count_all_results();
		

		
	}


	public function clear()
	{
		$aClassVars = get_class_vars(get_class($this));
		
		foreach ($aClassVars as $vVar => $vVal)
		{
			if (in_array($vVar, $this->_ignore_vars) === false)
			{
				$this->$vVar = false;
			}
		}
	}


	public function get_object() 
	{
		$aClassVars = get_class_vars(get_class($this));
		
		$aRet = array();
		
		foreach ($aClassVars as $vVar => $vVal)
		{
			if (in_array($vVar, $this->_ignore_vars) === false)
			{
				$aRet[$vVar] = '';
						
			}
		}
		
		return (object) $aRet;
	}	




}
// END Model Class

/* End of file Model.php */
/* Location: ./system/libraries/Model.php */